package com.example.zhangxun0527.MyShopDemo;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.zhangxun0527.R;

public class RegistActivity extends AppCompatActivity implements View.OnClickListener , CompoundButton.OnCheckedChangeListener {


    private EditText et_uesrname;
    private EditText et_password;
    private EditText et_repassword;
    private EditText et_phone;
    private TextView tv_hobby;
    private CheckBox singer;
    private CheckBox dance;
    private CheckBox dining;
    private CheckBox game;
    private RadioButton rb_mail;
    private RadioButton rb_femail;
    private RadioGroup rg_sex;
    private Button btn_login;
    String hobby="";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regist);
        initView();

    }


    private void initView() {
        et_uesrname = (EditText) findViewById(R.id.et_uesrname);
        et_password = (EditText) findViewById(R.id.et_password);
        et_repassword = (EditText) findViewById(R.id.et_repassword);
        et_phone = (EditText) findViewById(R.id.et_phone);
        tv_hobby = (TextView) findViewById(R.id.tv_hobby);
        singer = (CheckBox) findViewById(R.id.singer);
        dance = (CheckBox) findViewById(R.id.dance);
        dining = (CheckBox) findViewById(R.id.dining);
        game = (CheckBox) findViewById(R.id.game);
        rb_mail = (RadioButton) findViewById(R.id.rb_mail);
        rb_femail = (RadioButton) findViewById(R.id.rb_femail);
        rg_sex = (RadioGroup) findViewById(R.id.rg_sex);
        btn_login = (Button) findViewById(R.id.btn_login);

        btn_login.setOnClickListener(this);
        singer.setOnCheckedChangeListener(this);
        dance.setOnCheckedChangeListener(this);
        dining.setOnCheckedChangeListener(this);
        game.setOnCheckedChangeListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_login:
                submit();
                break;
        }
    }

    private void submit() {
        // validate
        String uesrname = et_uesrname.getText().toString().trim();
        String password = et_password.getText().toString().trim();
        String repassword = et_repassword.getText().toString().trim();
        String phone = et_phone.getText().toString().trim();
        String sex = "";

        if (TextUtils.isEmpty(uesrname) || TextUtils.isEmpty(password) || TextUtils.isEmpty(repassword) || TextUtils.isEmpty(phone)) {
            Toast.makeText(this, "输入内容不可为空", Toast.LENGTH_SHORT).show();
        } else {
            if (uesrname.length() < 5 || password.length() < 6 || repassword.length() < 6||phone.length()<10) {
                Toast.makeText(this, "输入的长度不符合，请重新输入", Toast.LENGTH_SHORT).show();

            } else {
                if (password.equals(repassword)) {
                    if (singer.isChecked() || dance.isChecked() || dining.isChecked() || game.isChecked()) {
                        if (rb_mail.isChecked() || rb_femail.isChecked()) {
                            User user = new User();
                            user.setUsername(uesrname);
                            user.setPassword(password);
                            user.setPhone(phone);
                            if (rb_mail.isChecked()) {
                                sex = "男";
                            } else {
                                sex = "女";
                            }
                            user.setSex(sex);
                            user.setHobby(hobby);
                            Intent intent = new Intent();
                            intent.putExtra("myUser", user);
                            setResult(100, intent);
                            finish();
                        } else {
                            Toast.makeText(this, "请选择性别！", Toast.LENGTH_SHORT).show();
                        }
                    } else {
                        Toast.makeText(this, "不能为空！请至少选择一个爱好", Toast.LENGTH_SHORT).show();
                    }

                } else {
                    Toast.makeText(this, "两次密码不一致，请重新输入", Toast.LENGTH_SHORT).show();
                }




            }


            // TODO validate success, do something


        }

    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()){
            case R.id.singer:
                hobby+="唱歌";
                break;
            case R.id.dance:
                hobby+="跳舞";
                break;
            case R.id.dining:
                hobby+="吃饭";
                break;
            case R.id.game:
                hobby+="玩游戏";
                break;
        }
    }
}
